export const base = {
    data () {
        return {
            modules:'users',
            modalAdd:false,
            editId:0,
            data: [],
            total:0,
            tree: [],
            ids:[],
            filter:{
                page:1,
                rows:4,//每页显示几条数据
                name:"",
                title:"",
                cateId:"",
            },
        };
    },
    methods: {
        //添加数据操作
        handleSubmit (info) {
            this.$refs[info].validate((valid) => {
                if (valid) {
                    console.log(this.editId)
                    if(this.editId.length>0){
                        //此时是修改
                        fetch(`http://localhost:3000/${this.modules}/data/${this.editId}`,{
                            method:'put',
                            headers:{
                                'Content-Type':'application/json'
                            },
                            body:JSON.stringify(this.formValidate)
                        }).then(res=>{
                            return res.json();
                        }).then(data=>{
                            this.getData();
                            this.$Message.success('修改成功!');
                            this.modalAdd = false;                           
                        });
                        
                    }else{
                        //此时是添加
                        fetch(`http://localhost:3000/${this.modules}/data`,{
                            method:'post',
                            headers:{
                                'Content-Type':'application/json'
                            },
                            body:JSON.stringify(this.formValidate)
                        }).then(res=>{
                            return res.json();
                        }).then(data=>{
                            this.getData();
                            this.$Message.success('添加成功!');
                            this.modalAdd = false;
                        })
                    }
                } else {
                    this.$Message.error('添加失败!');
                }
            })
        },
        handleReset (info) {
            this.$refs[info].resetFields();
        },
        editData (id) {
            fetch(`http://localhost:3000/${this.modules}/data/${id}`).then(res=>{
                return res.json();
            }).then(data=>{
                //console.log(data);
                this.formValidate = data;
                this.editId = id;
                this.modalAdd = true;
            });
        },
        //删除单条数据数据操作
        remove (id) {
            this.$Modal.confirm({
                title: '删除确认对话框',
                content: '<p>是否确认删除该条信息？</p>',
                onOk: () => {
                    fetch(`http://localhost:3000/${this.modules}/data/${id}`,{
                        method:'delete',
                    }).then(res=>{
                        return res.json();
                    }).then(data=>{
                        this.$Message.info('已删除');
                        this.getData();
                    });   
                },
                onCancel: () => {
                    this.$Message.info('已取消删除');
                }
            });
        },
        //删除多个
        removes(){
            this.$Modal.confirm({
                title: '删除确认对话框',
                content: '<p>是否确认删除选中的信息？</p>',
                onOk: () => {
                    fetch(`http://localhost:3000/${this.modules}/removes`,{
                        method:'post',
                        headers:{
                            'Content-Type':'application/json'
                        },
                        body:JSON.stringify({ids:this.ids})
                    }).then(res=>{
                        return res.json();
                    }).then(data=>{
                        this.$Message.info('选中的信息已删除');
                        this.getData();
                    });   
                },
                onCancel: () => {
                    this.$Message.info('已取消删除');
                }
            });
        },
        //将选中的多个数据的id存在ids里面
        onSelectionChangeHandler(selection){
            var selectionIds = [];
            for(let i=0;i<selection.length;i++){
                selectionIds.push(selection[i]._id);
            }
            this.ids = selectionIds;
            //console.log(this.ids);
        },
        //获取用户数据
        getData(){//定义获取数据库的方法
            var bodyString= JSON.stringify(this.filter);//这里把filter里面的信息转成字符串形式存下来
            //console.log(bodyString);
            fetch(`http://localhost:3000/${this.modules}/list`,{
                method:'post',
                headers:{
                    'Content-Type':"application/json"
                },
                body:bodyString//前台定义的数据传到后台去
            }).then(res=>{
                    return res.json();
                }).then(data=>{
                    //console.log(data)
                    this.data = data.rows;//这里赋值后台返回的数据。rows代表数据库里面的所有数据，total是分页里面的参数
                    this.total = data.total;
                    //console.log(data.rows)
                });
        },
        //分页操作
        onChangeHandler(page){
            this.filter.page = page;//获取分页参数，第几页
            this.getData();
        },
        onPageSizeChangeHandler(pageSize){
            //console.log(pageSize);
            this.filter.rows = pageSize;//获取一页显示几个
            this.getData();
        },
        //搜索功能
        searchData(){
            this.getData();
        },
        //树结构点击获取信息
        changeTree(ev){
            //console.log(ev);
            this.filter.cateId = ev[0]._id;
            this.getData();
        },
        //获取树结构的信息
        getTree(){
            fetch(`http://localhost:3000/cate/list/${this.type}`,{
                method:'get',
                headers:{
                    "Content-Type":"application/json"
                },
            }).then(res=>{
                return res.json();
            }).then(data=>{
                this.tree = data;
                //console.log(data)
            })
        }
    },
    created(){//钩子函数里面调用上面定义的获取数据的方法
        this.getData();
        this.getTree();
    },
}